package edu.wong.pro.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import edu.wong.pro.iservice.INews;

public class NewsAction extends ActionSupport{
	private INews iNews;

	public INews getiNews() {
		return iNews;
	}

	public void setiNews(INews iNews) {
		this.iNews = iNews;
	}
	
	//发帖
	public String relNews(){
		HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
		String newsTheme = request.getParameter("newsTheme");
		String newsContent = request.getParameter("newsContent");
		iNews.relNews(newsTheme, newsContent);
		return "ok";
	}
	
	//获取我的帖子
	public void getMyNews() throws IOException{
		HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		writer.println(iNews.getMyNews());
		writer.flush();
		writer.close();	
	}
	
	//获取帖子详细条目
	public void getNewsDetail() throws IOException{
		HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
		String newsId =  request.getParameter("newsId");
		HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		writer.print(iNews.getNewsDetail(newsId));
		writer.flush();
		writer.close();	
	}
	
	//主页获取前10条最新News
	public void getTopNews() throws IOException{
		HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		writer.print(iNews.getTopNews());
		writer.flush();
		writer.close();	
	}
}
